#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

version="$(curl -s "https://api.github.com/repos/klzgrad/naiveproxy/releases/latest" | jq '.tag_name' --raw-output)"
link="https://github.com/klzgrad/naiveproxy/releases/download/${version}/naiveproxy-${version}-linux-${NP_MACHINE}.tar.xz"

mkdir -p /etc/naive
touch /etc/naive/config.json
cat >"/etc/naive/config.json" <<-EOF
{
  "listen": "http://127.0.0.1:8081",
  "padding": "true"
}
EOF

cd $(mktemp -d)
wget -q --show-progress "${link}" -O naive.tar.xz
tar Jxvf naive.tar.xz && rm -rf naive.tar.xz
cd "naiveproxy-${version}-linux-${NP_MACHINE}"
mv naive /usr/bin/naive && chmod +x /usr/bin/naive

cat >"/etc/systemd/system/naive.service" <<-EOF
[Unit]
Description=NaïveProxy Service
Documentation=https://github.com/klzgrad/naiveproxy
After=network.target nss-lookup.target

[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/bin/naive /etc/naive/config.json
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable naive

echo
echo -e "${GREEN} Pemasangan naiveproxy pakej telah selesai. ${RESET}"
echo
